﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;

public partial class admin_AreaCategoryList : SitePage
{
    public string html = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    private void LoadData()
    {
        bool isExpand = true;
        //Load all Catgeory in Level 1
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        objCondition.Condition = " ParentId is null and Type=" + (byte)enmCategoryType.Area;
        busCategory objBus = new busCategory();
        CategoryCollection lstLevel1 = objBus.List(objCondition);//only return one object
        if (lstLevel1 != null && lstLevel1.Count > 0)
        {
            html = @"<table cellspacing='0' cellpadding='0' border='0'>
                        <tr>
                            <td>
                                  <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderminus2.gif' id='FFLD" + lstLevel1[0].CategoryID + @"'>
                            </td>
                            <td>
                                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                            </td>
                            " + CreateButtonActive(lstLevel1[0]) + @"
                            " + CreateDeleteButton(lstLevel1[0].CategoryID) + @"
                            <td nowrap=''>
                                    <a href='AreaCategoryDetail.aspx?act=edit&id=" + lstLevel1[0].CategoryID + @"'>" + lstLevel1[0].Name + @"</a>
                            </td>                           
                            </tr>
                       </table>";
            CategoryCollection lstChild = GetChild(lstLevel1[0]);
            int position = 1;
            for (int i = 0; i < lstChild.Count; i++)
            {
                Category objBreadCrum = lstChild[i];
                if (i == lstChild.Count - 1)
                    position = 3;
                else if (i == 0)
                    position = 1;
                else position = 2;
                html += BuildHTML(objBreadCrum, isExpand, position, lstChild.Count);
            }
        }
    }
    private string CreateTdCountry(Category objCate)
    {
        int count = 0;
        busCountryAreaCategory objBus = new busCountryAreaCategory();
        CountryAreaCategoryCollection lstResult = objBus.GetByCategoryId(objCate.CategoryID);
        if (lstResult != null && lstResult.Count > 0)
            count = lstResult.Count;
        return "<td><img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'></td><td align='center' width='20px'><a href='ProductList.aspx?cateId=" + objCate.CategoryID + "'>(" + count + ")</a></td>";
       

    }
    private string CreateButtonActive(Category objCate)
    {
        if (objCate.IsActive)
            return "<td align='center' width='20px'><input type='image' style='border-width: 0px;' onclick='DeActive(" + objCate.CategoryID + @")' src='../App_Themes/Admin/images/icons/tick.png' title='De active this item' ></td>";
        else
            return "<td align='center' width='20px'><input type='image' style='border-width: 0px;' onclick='Active(" + objCate.CategoryID + @")' src='../App_Themes/Admin/images/icons/publish_x.png' title='Active this item' ></td>";

        //<input type="image" style="border-width: 0px;" src="../App_Themes/Admin/images/icons/tick.png" title="Deactive this item" id="ctl00_content_rptData_ctl01_imbActive" name="ctl00$content$rptData$ctl01$imbActive">
    }
    private string CreateDeleteButton(int categoryID)
    {

        return "<td><input type='image' style='border-width: 0px;' onclick='DeleteCategory(" + categoryID + @")' src='../App_Themes/Admin/images/icons/cross_circle.png' title='Delete' ></td>";

    }
    private string CreateOrderButton(int position, int categoryID, int count)
    {
        string result = @"<td>
                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                </td>";
        if (count <= 1)
            return result;
        result += "<td align='center' valign='middle' width='25px'>";
        if (position == 1)
            result += "<input type='image' style='border-width: 0px;' onclick='OrderDown(" + categoryID + @")' src='../App_Themes/Admin/images/icons/downarrow.png' title='Move down' >";
        else if (position == 3)
            result += "<input type='image' style='border-width: 0px;' onclick='OrderUp(" + categoryID + @")' src='../App_Themes/Admin/images/icons/uparrow.png' title='Move up' >";
        else
        {
            result += "<input type='image' style='border-width: 0px;' onclick='OrderDown(" + categoryID + @")' src='../App_Themes/Admin/images/icons/downarrow.png' title='Move down' >";
            result += "<input type='image' style='border-width: 0px;' onclick='OrderUp(" + categoryID + @")' src='../App_Themes/Admin/images/icons/uparrow.png' title='Move up' >";
        }
        result += "</td><td><img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'></td>";
        return result;
    }
    private string BuildHTML(Category objData, bool isExpand, int position, int lstCount)
    {
        bool lastNode = false;
        //position: 1:at the begin in list,2: at the middle, 3: at the end
        if (position == 3)
            lastNode = true;
        string tdPadding = string.Empty;
        string result = string.Empty;
        for (int i = 2; i < objData.Level; i++)
        {
            tdPadding += @" <td>
                    <img height='22' width='19' src='../App_Themes/Admin/images/icons/i2.gif'>
                </td>";
        }

        //get list child for this object
        CategoryCollection lstChild = GetChild(objData);
        if (lstChild != null && lstChild.Count > 0)
        {
            string folderexpand = "folderplus2.gif";
            string expandImage = string.Empty;
            if (isExpand)
            {
                folderexpand = "folderminus2.gif";
            }
            if (objData.Level > 1 && lastNode)
            {
                expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lplus2.gif'>";
            }
            else
            {
                if (isExpand)
                {
                    // folderexpand = "folderminus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tminus2.gif'>";
                }
                else
                {
                    // folderexpand = "folderplus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tplus2.gif'>";
                }
            }
            // expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lplus2.gif'>";
            result += @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td  onclick=expandit(" + objData.CategoryID + @")>
                    " + expandImage + @"
                </td>
                <td>
                    <img height='22' width='16' id='FLD" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/" + folderexpand + @"'>
                </td>
                  " + CreateButtonActive(objData) + @"
                " + CreateDeleteButton(objData.CategoryID) + @"
                " + CreateOrderButton(position, objData.CategoryID, lstCount) + @"
                <td nowrap=''>
                    <a href='AreaCategoryDetail.aspx?act=edit&id=" + objData.CategoryID + @"'>" + objData.Name + @"</a>
                </td>
               
            </tr>
        </table>";
            if (isExpand)
                result += "<span id='SPAN" + objData.CategoryID + "' style='display:block;'>";
            else result += "<span id='SPAN" + objData.CategoryID + "' style='display: none;'>";
            int subposition = 1;
            for (int index = 0; index < lstChild.Count; index++)
            {
                if (index == lstChild.Count - 1)
                    subposition = 3;
                else if (index == 0)
                    subposition = 1;
                else subposition = 2;
                Category objBreadCrum = lstChild[index];
                result += BuildHTML(objBreadCrum, isExpand, subposition, lstChild.Count);
            }
            result += "</span>";
        }
        else
        {
            string expandImage = string.Empty;
            if (lastNode)
            {
                expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/l2.gif'>";
            }
            else expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/t2.gif'>";

            result = @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td>
                   " + expandImage + @"
                </td>
                <td>
                    <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderplus2.gif'>
                </td>
                " + CreateButtonActive(objData) + @"
                " + CreateDeleteButton(objData.CategoryID) + @"
                " + CreateOrderButton(position, objData.CategoryID, lstCount) + @"
                <td nowrap=''>
                    <a href='AreaCategoryDetail.aspx?act=edit&id=" + objData.CategoryID + @"'>" + objData.Name + @"</a>
                </td>
             " + CreateTdCountry(objData) + @"
            </tr>
        </table>";
        }
        return result;
    }
    private CategoryCollection GetChild(Category objData)
    {
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.Condition = " ParentId=" + objData.CategoryID;
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        busCategory objBus = new busCategory();
        CategoryCollection lstResult = objBus.List(objCondition);
        return lstResult;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (hidAction.Value == "1")//Delete
            Delete(hidCategoryId.Value);
        else if (hidAction.Value == "2")//Order Down
            OrderDown(hidCategoryId.Value);
        else if (hidAction.Value == "3")//Order Down
            OrderUp(hidCategoryId.Value);
        else if (hidAction.Value == "4")//Active
            Active(hidCategoryId.Value);
        else if (hidAction.Value == "5")//DeActive
            DeActive(hidCategoryId.Value);
        Response.Redirect("AreaCategoryList.aspx");
    }
    private void OrderDown(string cateId)
    {
        busCategory objBus = new busCategory();
        objBus.OrderDown(clsConvert.Int(cateId));
    }
    private void OrderUp(string cateId)
    {
        busCategory objBus = new busCategory();
        objBus.OrderUp(clsConvert.Int(cateId));
    }
    private void Delete(string cateId)
    {
        busCategory objBus = new busCategory();
        objBus.DeleteAreaCategoryTree(clsConvert.Int(cateId));
    }
    private void Active(string cateId)
    {
        busCategory objBus = new busCategory();
        objBus.Active(clsConvert.Int(cateId));
    }
    private void DeActive(string cateId)
    {
        busCategory objBus = new busCategory();
        objBus.DeActive(clsConvert.Int(cateId));
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AreaCategoryDetail.aspx");
    }
}